// pages/control/game_of_point/game_of_point.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    erroClickFlag : 0,
    blockArry: [{ chosePo:[1,0,0] , clickFlag : [0,0,0]}]
  },
  // 单击方块的事件
  clickadd: function (event){
    var that = this
    var array = that.roundArray()
    console.log(array)
    var index = event.currentTarget.dataset.index
    var one = event.currentTarget.dataset.one
    console.log(index, one)
    that.checkWhiteIsClick(index, one)
    that.data.blockArry[index].clickFlag[one] = 1;
    that.data.blockArry.unshift({ chosePo: array, clickFlag : [0,0,0]});
    var pop_block = that.data.blockArry.pop();
    that.checkNoClickOut(pop_block);
    that.setData({
      blockArry: that.data.blockArry
    })
  },
  // 检查数组是否被点击，否则表示游戏失败
  checkNoClickOut:function(ob){
    for (var item in ob.chosePo){
      
    }
    if (!ob.clickFlag[index]){
      console.log("U are fail")
      this.gameEndShow()
    }
  },
  checkWhiteIsClick:function(row,clum){
    console.log(this.data.blockArry[row].chosePo[clum])
    if (this.data.blockArry[row].chosePo[clum] != this.data.blockArry[row].clickFlag[clum]){
      console.log('u are fail')
      this.gameEndShow();
    }
  },
  // 随机生成1/0数据
  roundArray:function(){
    // console.log(Math.round(Math.random()))
    var arra = []
    while (1){
      if(Math.round(Math.random())){
        arra.push(1)
        while (arra.length < 3) { arra.push(0)}
        return arra
      }else{
        if (arra.length == 2) { arra.push(1); return arra}
        arra.push(0)
      }
    }
  },
  gameEndShow: function () {
    wx.showModal({
      title: 'Erro',
      content: '游戏失败',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  // 添加初始化数据，有5行块
  onReady: function () {
    var that = this
    var count = 4;
    while(count){
      var array = that.roundArray()
      that.data.blockArry.unshift({ chosePo: array, clickFlag: 0 });
      that.setData({
        blockArry: that.data.blockArry
      })
      count = count - 1;
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})